<?php
// php最大执行时间设置为：半个小时，php原来默认为30秒，爬不完
ini_set('max_execution_time', '0');
include './vendor/autoload.php';// 使用composer安装后引入目录
use Medoo\Medoo;
use QL\QueryList;
include_once 'class/Mysql.php';

try {
    $mysql = Mysql::getMysql();
    $obj = $mysql->query("select * from source where status=0 limit 5");
    $result =$obj->fetchAll();
    $baseUrl='https://www.stickpng.com';
    $saveDir='./images/';

    $rules = array(
        'title' => array('h1', 'text'),    //获取纯文本格式的标题,并调用回调函数1
        'keywords'=>array('.keywords','text'),
        'imgurl' => array('img[itemprop="contentUrl"]', 'src'),
    );


    foreach ($result as $v){
        //采集某页面所有的图片
        $data = QueryList::get($v['url'])->rules($rules)->queryData();
        if(empty($data["keywords"]) && empty($data['imgurl'])) {
            file_put_contents('logs/empty.log',$url.PHP_EOL,FILE_APPEND);
            continue;
        }

        $title=str_ireplace('Download','',$data['title']);
        $title=str_ireplace('transparent','',$title);
        $title=str_ireplace('PNG','',$title);
        $keywords = explode("\n",trim($data['keywords']));

        $res['id']=$v['id'];
        $res['title']=trim($title);
        $res['keywords'] = implode(',',$keywords);
        $res['imgurl']=$data['imgurl'];
        if(file_exists($saveDir.basename($data['imgurl']))){
            file_put_contents('logs/exits.log',json_encode($res,258).PHP_EOL,FILE_APPEND);
            continue;
        }else{
            $imgInfo = download_image($baseUrl . $data['imgurl'], $saveDir,basename($data['imgurl']));
            //下载图片
            $res['localFileName'] = $imgInfo["fileName"];
            $res['localUrl'] = ltrim($imgInfo["savePath"], '.');
            list($width, $height, $type, $attr) = getimagesize($baseUrl . $data['imgurl']);
            $res['resolution']=$width.'x'.$height;
            $res['size'] =trans_byte(get_headers($baseUrl . $data['imgurl'],true)['Content-Length']);
        }
        $obj = $mysql->query("update source set status =1 where id= ".$v['id']);
        $result =$obj->execute();
        error_log(json_encode($res,258) . PHP_EOL, 3, 'data/data.csv');
    }

}catch (Exception $e) {
    $exceptionlog = '文件 ['.$e->getFile().'] 第 '.$e->getLine().' 行出现未处理的异常'.PHP_EOL;
    $exceptionlog .= '代码:'.$e->getCode().PHP_EOL;
    $exceptionlog .= '信息:'.$e->getMessage().PHP_EOL;
    $exceptionlog .= '跟踪信息：'.$e->getTraceAsString();
    //记录错误
    error_log(date('Y-m-d H:i:s') . print_r($exceptionlog, 1) . PHP_EOL, 3, 'logs/'.date('Y-m-d').'_'.'exception.log');
}




function trans_byte($byte){

    $KB = 1024;

    $MB = 1024 * $KB;

    $GB = 1024 * $MB;

    $TB = 1024 * $GB;

    if ($byte < $KB) {

        return $byte . "B";

    } elseif ($byte < $MB) {

        return round($byte / $KB, 2) . "KB";

    } elseif ($byte < $GB) {

        return round($byte / $MB, 2) . "MB";

    } elseif ($byte < $TB) {

        return round($byte / $GB, 2) . "GB";

    } else {

        return round($byte / $TB, 2) . "TB";

    }

}
/**
 * 下载远程图片保存到本地
 * @access public
 * @author      lxhui<772932587@qq.com>
 * @since 1.0
 * @return array
 * @params string $url 远程图片地址
 * @params string $save_dir 需要保存的地址
 * @params string $filename 保存文件名
 */
function download_image($url, $save_dir = '',$filename='')
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    $file = curl_exec($ch);
    curl_close($ch);
    $filename = pathinfo($url, PATHINFO_BASENAME);
    $resource = fopen($save_dir . $filename, 'a');
    fwrite($resource, $file);
    fclose($resource);
    unset($file,$url);
    return array('fileName'=>$filename,'savePath'=>$save_dir.$filename,'error'=>0);
}

?>